//14.7 - Movement.cpp - Mark Lee - Prima Publishing #include "Movement.h" BOOL PointInRECT(int x, int y, RECT* rc) //returns true if the point is inside the RECT { //big if statement to test if (x < rc->right && x > rc->left && y < rc->bottom && y > rc->top) return true; //return false if not inside return false; } BOOL RECTinRECT(RECT* one, RECT* two) //returns true if two RECTs are inside eachother { //tests if any of the four corners of one are inside two if (PointInRECT(one->left, one->top, two)) return true; if (PointInRECT(one->left, one->bottom, two)) return true; if (PointInRECT(one->right, one->top, two)) return true; if (PointInRECT(one->right, one->bottom, two)) return true; //return false if not inside return false; } BOOL isInCity(RECT* pos) //returns true if pos is within any town boundaries { for (int i = 0; i <13; i++) //go through each tow one by one //and test with RECTinRECT if (RECTinRECT(pos,&towns[i].position)) return true; return false; } bool landCollision(int x, int y, string filename) //returns true if the point at (x,y) is land //uses filname as the name of a bitmap file to read from { y = 599 - y;//switch to computer coordinates //if (x,y) is offscreen then no collision if(x < 0 || y > 599) return false; if(y < 0 || x > 799) return false; //perform file io ifstream file(filename.c_str(), ios::in|ios::binary); //start redaing from the correct position file.seekg(3 * y * 800 + 3 * x + 54); //three bytes for each pixel //3 - Red, 2 - Green, 1 - Blue char array[3]; file.read(array, 3); //end file io file.close(); //if green is less than blue then we are in the sea if((int)array[1] < (int)array[0]) return false; //otherwise there is a collision return true; } void DoMove() //moves the ship based on what keys were pressed { //process which direction to move based on input int dir = ship->GetDirection(); if (moveUp == true) dir = 3; else if (moveDown == true) dir = 1; else if (moveLeft == true) dir = 2; else if (moveRight == true) dir = 0; else return; //actually move the ship ship->Move(dir); } RECT makeRECT(int x1, int y1, int x2, int y2) //constructs a RECT from 4 integers { //the theory: code it once, never have to code it again RECT temp; temp.left = x1; temp.top = y1; temp.right = x2; temp.bottom = y2; return temp; }